package org.mili.ticketeer.ui.role;

import org.mili.ticketeer.dto.Role;
import org.mili.ticketeer.service.RoleService;
import org.mili.ticketeer.service.ServiceException;
import org.mili.ticketeer.service.ServiceFactory;
import org.mili.ticketeer.ui.SimpleTableModel;

import java.util.List;

/**
 */
public class RoleTableModel extends SimpleTableModel<Role> {
    private RoleService roleService = ServiceFactory.getService(RoleService.class);

    public RoleTableModel() {
        super(new String[]{"No.", "Name"});
        update();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Role role = get(rowIndex);
        switch(columnIndex) {
            case 0:
                return rowIndex + 1;
            case 1:
                return role.getName();
            default: throw new IllegalStateException("no column index for: " + columnIndex);
        }
    }

    @Override
    public List<Role> load() {
        try {
            return roleService.getRoles();
        } catch (ServiceException e) {
            throw new IllegalStateException(e);
        }
    }
}
